

from itertools import product

bi = range(2)

def rot1(mat):
    [
    [[a1, a2], [a4, a3]],
    [[a5, a6], [a8, a7]],
    [[a9, a10], [a12, a11]],
    ] = mat
    return [
    [[a4, a1], [a3, a2]],
    [[a8, a5], [a7, a6]],
    [[a12, a9], [a11, a10]],
    ]
    
def rot3(mat):
    [
    [[a4, a1], [a3, a2]],
    [[a8, a5], [a7, a6]],
    [[a12, a9], [a11, a10]],
    ] = mat
    return [
    [[a1, a2], [a4, a3]],
    [[a5, a6], [a8, a7]],
    [[a9, a10], [a12, a11]],
    ]
    
def rot2(mat):
    [
    [[a1, a2], [a4, a3]],
    [[a5, a6], [a8, a7]],
    [[a9, a10], [a12, a11]],
    ] = mat
    return [
    [[a3, a4], [a2, a1]],
    [[a7, a8], [a6, a5]],
    [[a11, a12], [a10, a9]],
    ]

def flipx(mat):
    [
    [[a1, a2], [a4, a3]],
    [[a5, a6], [a8, a7]],
    [[a9, a10], [a12, a11]],
    ] = mat
    return [
    [[a2, a1], [a3, a4]],
    [[a6, a5], [a7, a8]],
    [[a10, a9], [a11, a12]],
    ]

def count(mat):
    return sum(mat[i][j][k] for i, j, k in product(range(3), bi, bi))

acc = []
sort_help = []
# ~ for a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12 in product(bi, bi, bi, bi, bi, bi, bi, bi, bi, bi, bi, bi):
for a12, a11, a10, a9, a8, a7, a6, a5 , a4, a3, a2, a1 in product(bi, bi, bi, bi, bi, bi, bi, bi, bi, bi, bi, bi):
    mat = [
    [[a1, a2], [a4, a3]],
    [[a5, a6], [a8, a7]],
    [[a9, a10], [a12, a11]],
    ]
    m1 = rot1(mat)
    m2 = rot2(mat)
    m3 = rot3(mat)
    f1 = flipx(mat)
    f2 = flipx(m1)
    f3 = flipx(m2)
    f4 = flipx(m3)
    if any(map(acc.__contains__, [m1, m2, m3, f1,f2, f3,f4])):
        pass
    else:
        level = 4
        if mat == m1:
            level = 1
        elif mat == f1 or mat == f3:
            level = 2
        elif mat == m2:
            level = 3
        sort_help.append((level, count(mat), len(acc)))
        acc.append(mat)
        # ~ print(mat)
    
print(len(acc))

sort_help.sort()

# ~ for x in sort_help:
    # ~ print(x)

sacc = [acc[x[2]] for x in sort_help]

from mat_encode import en
with open("building12b.txt", 'w') as f:
    for mat in sacc:
        s = en(mat)
        f.write(s+'\n')
